﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.Threading;
using EntityFramework.Extensions;
using System.Data.Objects.SqlClient;
namespace Webcar.Controllers
{
    public partial class CarController
    {
        public ActionResult OilList()
        {
            return View();
        }

        public ActionResult OilListPartialView(string id)
        {
            return PartialView();
        }
        public ActionResult OilEdit(string carno, string month)
        {
            BS_CAR_MILE cost = null;
            if (carno.IsNotNull())
            {
                cost = WebcarDataProvider.DB.BS_CAR_MILE.Find(carno, month);
            }
            if (cost == null)
            {
                cost = new BS_CAR_MILE();
                cost.CAR_NO = carno;
                
                cost.NY = DateTime.Now.ToString("yyyy-MM");
            }
            ViewBag.RecordNo = null;
            ViewBag.CarOilList = null;
            if (cost.CAR_NO.IsNotNull())
            {
                var car = WebcarDataProvider.DB.BS_CAR_RECORD.Find(cost.CAR_NO);
                if (car != null)
                {
                    ViewBag.RecordNo = car.RECORD_NO;
                    ViewBag.DeptCode = car.DEPT_CODE;
                }
                ViewBag.CarOilList = WebcarDataProvider.DB.BS_CAR_MILE.Where(a => a.CAR_NO == cost.CAR_NO && a.NY.Substring(0, 4) == month.Substring(0, 4)).OrderBy(a => a.NY).ToArray();
            }
            return View(model: cost);
        }
        public ActionResult OilSave(BS_CAR_MILE cost)
        {
            if (cost.NY.IsNull() || cost.CAR_NO.IsNull())
            {
                return Content("请选择车辆和输入月份。");
            }
            if ((cost.NY + "-01").IsDate() == false)
            {
                return Content("月份格式错误。格式示例：2010-10");
            }
            using (var DB = new WebcarContext())
            {
                var item = DB.BS_CAR_MILE.Find(cost.CAR_NO, cost.NY);
                if (item == null)
                {
                    DB.BS_CAR_MILE.Add(cost);
                }
                else
                {
                    cost.CopyPropertyTo(item);
                }
                DB.SaveChanges();
            }
            return RedirectToAction("OilList");
        }

        public ActionResult OilDelete(string car_no, string month)
        {
            using (var DB = new WebcarContext())
            {
                //CurrentUser.CheckPermission(PermissionManager.编辑单车成本);
                var cost = DB.BS_CAR_MILE.Delete(a => a.CAR_NO == car_no && a.NY == month);

                DB.SaveChanges();
            }
            //return new RedirectResult()
            //return RedirectToRoute("List", "Insure", new { id = id });
            return RedirectToAction("OilList", "Car");
        }
        public ActionResult OilSummary(string year,string dept)
        {
            ViewBag.select_year = year.IsNull()?DateTime.Now.Year.ToString():year;
            if (year.IsNull()) return View();
            if (dept == "ALL")
            {
                var list = from p in WebcarDataProvider.DB.BS_CAR_MILE
                           join
                               p2 in WebcarDataProvider.DB.BS_CAR_RECORD
                               on p.CAR_NO equals p2.CAR_NO
                           where p.NY.Substring(0, 4) == year
                           group p by p2.DEPT_CODE into result
                           select new
                           {
                               dept_code = result.Key,
                               mile = result.Sum(a => a.MILE == null ? 0 : a.MILE),
                               fuel_num = result.Sum(a => a.FUEL_NUM == null ? 0 : a.FUEL_NUM),

                               mile_01 = result.Sum(a => a.NY.Substring(5, 2) == "01" ? a.MILE : 0),
                               mile_02 = result.Sum(a => a.NY.Substring(5, 2) == "02" ? a.MILE : 0),
                               mile_03 = result.Sum(a => a.NY.Substring(5, 2) == "03" ? a.MILE : 0),
                               mile_04 = result.Sum(a => a.NY.Substring(5, 2) == "04" ? a.MILE : 0),
                               mile_05 = result.Sum(a => a.NY.Substring(5, 2) == "05" ? a.MILE : 0),
                               mile_06 = result.Sum(a => a.NY.Substring(5, 2) == "06" ? a.MILE : 0),
                               mile_07 = result.Sum(a => a.NY.Substring(5, 2) == "07" ? a.MILE : 0),
                               mile_08 = result.Sum(a => a.NY.Substring(5, 2) == "08" ? a.MILE : 0),
                               mile_09 = result.Sum(a => a.NY.Substring(5, 2) == "09" ? a.MILE : 0),
                               mile_10 = result.Sum(a => a.NY.Substring(5, 2) == "10" ? a.MILE : 0),
                               mile_11 = result.Sum(a => a.NY.Substring(5, 2) == "11" ? a.MILE : 0),
                               mile_12 = result.Sum(a => a.NY.Substring(5, 2) == "12" ? a.MILE : 0),

                               fuel_01 = result.Sum(a => a.NY.Substring(5, 2) == "01" ? a.FUEL_NUM : 0),
                               fuel_02 = result.Sum(a => a.NY.Substring(5, 2) == "02" ? a.FUEL_NUM : 0),
                               fuel_03 = result.Sum(a => a.NY.Substring(5, 2) == "03" ? a.FUEL_NUM : 0),
                               fuel_04 = result.Sum(a => a.NY.Substring(5, 2) == "04" ? a.FUEL_NUM : 0),
                               fuel_05 = result.Sum(a => a.NY.Substring(5, 2) == "05" ? a.FUEL_NUM : 0),
                               fuel_06 = result.Sum(a => a.NY.Substring(5, 2) == "06" ? a.FUEL_NUM : 0),
                               fuel_07 = result.Sum(a => a.NY.Substring(5, 2) == "07" ? a.FUEL_NUM : 0),
                               fuel_08 = result.Sum(a => a.NY.Substring(5, 2) == "08" ? a.FUEL_NUM : 0),
                               fuel_09 = result.Sum(a => a.NY.Substring(5, 2) == "09" ? a.FUEL_NUM : 0),
                               fuel_10 = result.Sum(a => a.NY.Substring(5, 2) == "10" ? a.FUEL_NUM : 0),
                               fuel_11 = result.Sum(a => a.NY.Substring(5, 2) == "11" ? a.FUEL_NUM : 0),
                               fuel_12 = result.Sum(a => a.NY.Substring(5, 2) == "12" ? a.FUEL_NUM : 0),

                           };
                var list2 = from p in list
                            join p2 in CurrentUser.UserGrantedDept
                                on p.dept_code equals p2.deptcode
                            select p;
                return View(list2.ToArray());
            }
            else
            {
                var list = from p in WebcarDataProvider.DB.BS_CAR_MILE
                           join
                               p2 in WebcarDataProvider.DB.BS_CAR_RECORD
                               on p.CAR_NO equals p2.CAR_NO
                           where p.NY.Substring(0, 4) == year&&p2.DEPT_CODE==dept
                           group p by p2.CHILD_DEPT into result
                           select new
                           {
                               dept_code = result.Key,
                               mile = result.Sum(a => a.MILE == null ? 0 : a.MILE),
                               fuel_num = result.Sum(a => a.FUEL_NUM == null ? 0 : a.FUEL_NUM),

                               mile_01 = result.Sum(a => a.NY.Substring(5, 2) == "01" ? a.MILE : 0),
                               mile_02 = result.Sum(a => a.NY.Substring(5, 2) == "02" ? a.MILE : 0),
                               mile_03 = result.Sum(a => a.NY.Substring(5, 2) == "03" ? a.MILE : 0),
                               mile_04 = result.Sum(a => a.NY.Substring(5, 2) == "04" ? a.MILE : 0),
                               mile_05 = result.Sum(a => a.NY.Substring(5, 2) == "05" ? a.MILE : 0),
                               mile_06 = result.Sum(a => a.NY.Substring(5, 2) == "06" ? a.MILE : 0),
                               mile_07 = result.Sum(a => a.NY.Substring(5, 2) == "07" ? a.MILE : 0),
                               mile_08 = result.Sum(a => a.NY.Substring(5, 2) == "08" ? a.MILE : 0),
                               mile_09 = result.Sum(a => a.NY.Substring(5, 2) == "09" ? a.MILE : 0),
                               mile_10 = result.Sum(a => a.NY.Substring(5, 2) == "10" ? a.MILE : 0),
                               mile_11 = result.Sum(a => a.NY.Substring(5, 2) == "11" ? a.MILE : 0),
                               mile_12 = result.Sum(a => a.NY.Substring(5, 2) == "12" ? a.MILE : 0),
                               fuel_01 = result.Sum(a => a.NY.Substring(5, 2) == "01" ? a.FUEL_NUM : 0),
                               fuel_02 = result.Sum(a => a.NY.Substring(5, 2) == "02" ? a.FUEL_NUM : 0),
                               fuel_03 = result.Sum(a => a.NY.Substring(5, 2) == "03" ? a.FUEL_NUM : 0),
                               fuel_04 = result.Sum(a => a.NY.Substring(5, 2) == "04" ? a.FUEL_NUM : 0),
                               fuel_05 = result.Sum(a => a.NY.Substring(5, 2) == "05" ? a.FUEL_NUM : 0),
                               fuel_06 = result.Sum(a => a.NY.Substring(5, 2) == "06" ? a.FUEL_NUM : 0),
                               fuel_07 = result.Sum(a => a.NY.Substring(5, 2) == "07" ? a.FUEL_NUM : 0),
                               fuel_08 = result.Sum(a => a.NY.Substring(5, 2) == "08" ? a.FUEL_NUM : 0),
                               fuel_09 = result.Sum(a => a.NY.Substring(5, 2) == "09" ? a.FUEL_NUM : 0),
                               fuel_10 = result.Sum(a => a.NY.Substring(5, 2) == "10" ? a.FUEL_NUM : 0),
                               fuel_11 = result.Sum(a => a.NY.Substring(5, 2) == "11" ? a.FUEL_NUM : 0),
                               fuel_12 = result.Sum(a => a.NY.Substring(5, 2) == "12" ? a.FUEL_NUM : 0)
                           };
                var list2 = from p in list
                            join p2 in CurrentUser.UserGrantedDept
                                on p.dept_code equals p2.deptcode
                            select p;
                return View(list2.ToArray());
            }
            
        }
    }
}